package com.student.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.student.DBConn;
import com.student.entities.UserEntity;
import com.student.util.MD5;

public class UserService {
	public static UserEntity getUser(String userName) throws SQLException {
		UserEntity user = new UserEntity();
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_user where user_name like '"
				+ userName + "'";
		System.out.println(sql);
		ResultSet rs = stmt.executeQuery(sql);
		rs.next();
		user.setId(rs.getInt("user_id"));
		user.setName(rs.getString("user_name"));
		user.setAddr(rs.getString("user_addr"));
		user.setEmail(rs.getString("user_email"));
		user.setPassword(rs.getString("user_password"));
		user.setTel(rs.getString("user_tel"));
		user.setRegTime(rs.getDate("user_regtime").toString());
		user.setSex(rs.getString("user_sex"));
		user.setCampus(rs.getString("user_campus"));
		user.setHeadImgUrl(rs.getString("user_headImg"));
		user.setOrigin(rs.getString("user_origin"));
		rs.close();
		conn.close();
		return user;
	}

	// 检查是否有某一用户
	public static boolean checkUser(String userName) throws SQLException {
		Connection conn = DBConn.getConn();
		boolean result = false;
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_user where user_name like '"
				+ userName + "'";
		System.out.println(sql);
		ResultSet rs = stmt.executeQuery(sql);
		if (rs.first()) {
			result = true;
		}
		rs.close();
		conn.close();
		return result;
	}

	// 检查是否有某一用户
	public static boolean checkUserByEmail(String email) throws SQLException {
		Connection conn = DBConn.getConn();
		boolean result = false;
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_user where user_email like '" + email
				+ "'";
		System.out.println(sql);
		ResultSet rs = stmt.executeQuery(sql);
		if (rs.first()) {
			result = true;
		}
		rs.close();
		conn.close();
		return result;
	}

	// 检查是否有其他用户使用了某个邮箱
	public static boolean checkUserByEmail(String userName, String email)
			throws SQLException {
		Connection conn = DBConn.getConn();
		boolean result = false;
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_user where user_name not like '"
				+ userName + "' and  user_email like '" + email + "'";
		System.out.println(sql);
		ResultSet rs = stmt.executeQuery(sql);
		if (rs.first()) {
			result = true;
		}
		rs.close();
		conn.close();
		return result;
	}

	// 检查是否有某一用户
	public static boolean checkUserByUserName(String userName)
			throws SQLException {
		Connection conn = DBConn.getConn();
		boolean result = false;
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_user where user_name like '"
				+ userName + "'";
		System.out.println(sql);
		ResultSet rs = stmt.executeQuery(sql);
		if (rs.first()) {
			result = true;
		}
		rs.close();
		conn.close();
		return result;
	}

	// 检查是否有某一用户
	public static boolean checkUserByPhone(String phone) throws SQLException {
		Connection conn = DBConn.getConn();
		boolean result = false;
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_user where user_tel like '" + phone
				+ "'";
		System.out.println(sql);
		ResultSet rs = stmt.executeQuery(sql);
		if (rs.first()) {
			result = true;
		}
		rs.close();
		conn.close();
		return result;
	}

	// 添加用户
	public static boolean addUser(UserEntity user) throws SQLException {
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String regtime = sdf.format(new Date());
		String sql = "insert into shop_user values(null,'" + user.getName()
				+ "','" + user.getEmail() + "','" + user.getHeadImgUrl()
				+ "','" + user.getPassword() + "','" + user.getAddr() + "','"
				+ user.getCampus() + "','" + user.getTel() + "','"
				+ user.getOrigin() + "','" + user.getSex() + "','" + regtime
				+ "',0,'" + regtime + "',0,null,null)";
		System.out.println(sql);
		int result = stmt.executeUpdate(sql);
		conn.close();
		return result > 0 ? true : false;
	}

	// 验证用户登录
	public static String confirm(String userName, String password)
			throws SQLException {
		Connection conn = DBConn.getConn();
		String result = "failed";
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_user where (user_name = '" + userName
				+ "' or user_email = '" + userName + "'  or user_tel = '"
				+ userName + "' ) and user_password = '" + password + "'";
		System.out.println(sql);
		ResultSet rs = stmt.executeQuery(sql);
		if (rs.first()) {
			result = rs.getString("user_name");
		}
		rs.close();
		conn.close();
		return result;
	}

	public static void main(String args[]) throws SQLException {
		System.out.println(confirm("吟风", MD5.Encrypt("liwentao")));
	}
}
